#include "layoutwidget.h"

LayoutWidget::LayoutWidget(Layout layout, int screen_id, int partition_id, BaseShowWidget *parent) :
                           BaseShowWidget(parent), m_layout(layout), m_screen_id(screen_id), m_partition_id(partition_id)
{
    //为每一个layout注册点击事件回调函数，通过全局观察者发送消息
    GlobalObserver::getGlobalObserver()->attach(loss_select_msg, this, SLOT(setLossSelect()));

    for (int i = 0; i < m_layout.items.size(); i++)
        addItem(m_layout.items.at(i));
}

void LayoutWidget::setSelectStyle(bool select)//设置控件选择样式
{
    if (select)
    {
        setStyleSheet("border:2px solid orange");
        //setStyleSheet("background-color:rgb(225,255,255);border:2px solid orange");
        GlobalObserver::getGlobalObserver()->notify(layout_select_msg, m_screen_id, m_partition_id, m_layout.id);
        parentWidget()->raise();
    } else
    {
        setStyleSheet("border:none;");
        //setStyleSheet("border:none;background-color:transparent");
    }
}

void LayoutWidget::setAllChildrenLossSelect()
{
    setLossSelect();

    for (auto it = m_items_data.begin(); it != m_items_data.end(); it++)
    {
        it.value().item_widget->setAllChildrenLossSelect();
    }
}

void LayoutWidget::selectChild(int item_id, QString cab_pos_item_str)
{
    m_items_data[cab_pos_item_str + QString("$$%1").arg(item_id)].item_widget->setSelected();
}

void LayoutWidget::addCabPos(int item_id, CabPosItem add_cabpositem, QString cab_pos_item_str)
{
    ItemShowData one_data;
    one_data.item_widget = new ItemWidget(add_cabpositem, m_screen_id, m_partition_id, m_layout.id,
                                          item_id, cab_pos_item_str, this);
    one_data.item_widget->setGeometry(add_cabpositem.x, add_cabpositem.y, add_cabpositem.width, add_cabpositem.height);
    //one_data.item_widget->setText(QString("item_id:%1\n").arg(item_id) + cab_pos_item_str);

    one_data.cab_pos_item_data = add_cabpositem;
    m_items_data[cab_pos_item_str + QString("$$%1").arg(item_id)] = one_data;

    one_data.item_widget->setSelected();
    one_data.item_widget->show();
}

void LayoutWidget::addItem(Item add_item)
{
    for (auto it = add_item.cabpositems.begin(); it != add_item.cabpositems.end(); it++)
    {
        addCabPos(add_item.id, it.value(), it.key());
    }
}

void LayoutWidget::updateLayout(Layout layout)
{
    m_layout = layout;
    for (int i = 0; i < layout.items.size(); i++)
    {
        for (auto it = layout.items.at(i).cabpositems.begin(); it != layout.items.at(i).cabpositems.end(); it++)
        {
            m_items_data[it.key() + QString("$$%1").arg(layout.items.at(i).id)].cab_pos_item_data = it.value();
            m_items_data[it.key() + QString("$$%1").arg(layout.items.at(i).id)].item_widget->loadAttribute(it.value());
        }
    }
}

void LayoutWidget::delItem(int item_id)
{
    for (auto it = m_items_data.begin(); it != m_items_data.end();)
    {
        if (item_id == it.value().cab_pos_item_data.id)
        {
            delete it.value().item_widget;

            for (auto iter = m_layout.items.begin(); iter != m_layout.items.end();)
            {
                if ((*iter).id == item_id)
                    iter = m_layout.items.erase(iter);
                else
                    ++iter;
            }

            it = m_items_data.erase(it);
        }
        else
        {
            ++it;
        }
    }
}

void LayoutWidget::delCabPos(int item_id, QString cab_pos_item_str)
{
    for (auto it = m_items_data.begin(); it != m_items_data.end();)
    {
        if (cab_pos_item_str + QString("$$%1").arg(item_id) == it.key())
        {
            delete it.value().item_widget;

            for (auto iter = m_layout.items.begin(); iter != m_layout.items.end();)
            {
                if ((*iter).id == item_id)
                    iter = m_layout.items.erase(iter);
                else
                    ++iter;
            }

            it = m_items_data.erase(it);
        }
        else
        {
            ++it;
        }
    }
}
